import {
  auditUserDeliverResume, checkBatchUserDeliverResume, checkUserDeliverResume,
  getAllResumeAudit,
  getAllUsersFileResume,
  getAllUsersPlatformResume, getUserDeliverInfoById,
  getUserResumeInfoByResumeId,
  removeFilesByIds,
  removeUserPlatformResumeInfo
} from '@/api/resume'
import { Promise } from 'jszip/lib/external'

const state = {
  platforms: [],
  platforms_total: 0,
  files: [],
  files_total: 0,
  audits: [],
  audits_total: 0,
}

const mutations = {

  SET_PLATFORM(state, data) {
    state.platforms = data.records
    state.platforms_total = data.total
  },

  SET_FILES(state, data) {
    state.files = data.records
    state.files_total = data.total
  },

  SET_AUDIT(state, data) {
    state.audits = data.records
    state.audits_total = data.total
  }
}

const actions = {

  getAllUsersPlatformResume({commit}, data) {
    return new Promise((resolve, reject) => {
      getAllUsersPlatformResume(data).then((resp) => {
        if (resp.data.code === 1) {
          resolve(true)
          commit("SET_PLATFORM", resp.data.data)
        } else if (resp.data.code === 0) {
          reject(resp.data.msg)
        }
        reject("error")
      }).catch((error) => {
        reject(error)
      })
    })
  },

  getAllUsersFileResume({commit}, data) {
    return new Promise((resolve, reject) => {
      getAllUsersFileResume(data).then((resp) => {
        if (resp.data.code === 1) {
          resolve(true)
          commit("SET_FILES", resp.data.data)
        } else if (resp.data.code === 0) {
          reject(resp.data.msg)
        }
        reject("error")
      }).catch((error) => {
        reject(error)
      })
    })
  },

  removeUserPlatformResumeInfo({commit}, id) {
    return new Promise((resolve, reject) => {
      removeUserPlatformResumeInfo(id)
        .then((resp) => {
          if (resp.data.data) {
            resolve(true)
          }
          reject(false)
        })
        .catch((error) => { reject(error) })
        .finally(() => {})
    })
  },

  getUserResumeInfoByResumeId({commit}, id) {
    return new Promise((resolve, reject) => {
      getUserResumeInfoByResumeId(id)
        .then((resp) => {
          const {data} = resp
          if (data.code === 1) {
            resolve(data.data)
          } else if (data.code === 0) {
            reject(data.msg)
          }
          reject(false)
        })
        .catch((error) => {
          reject(false)
        })
    })
  },

  removeFilesByIds({commit}, data) {
    return new Promise((resolve, reject) => {
      removeFilesByIds(data).then(r => {
        if (r.data.code === 1) {
          resolve(r.data.data)
        } else if (r.data.code === 0 ) {
          reject(r.data.msg)
        }
        reject("error")
      }).catch((error) => {
        reject(error)
      })
    })
  },

  getAllResumeAudit({commit}, data) {
    return new Promise((resolve, reject) => {
      getAllResumeAudit(data).then((resp) => {
        if (resp.data.code === 1) {
          resolve(true)
          commit("SET_AUDIT", resp.data.data)
        } else if (resp.data.code === 0) {
          reject(resp.data.msg)
        }
        reject("error")
      }).catch((error) => {
        reject(error)
      })
    })
  },

  checkUserDeliverResume({commit}, id) {
    return new Promise((resolve, reject) => {
      checkUserDeliverResume(id).then(r => {
        if (r.data.code === 1) {
          resolve(r.data.data)
        }
        reject("投递记录不存在 请重新尝试！")
      }).catch((error) => {
        reject(error)
      })
    })
  },

  auditUserDeliverResume({commit}, data) {
    return new Promise((resolve, reject) => {
      auditUserDeliverResume(data).then(r => {
        if (r.data.code === 1) {
          resolve(r.data.data)
        } else if (r.data.code === 0 ) {
          reject(r.data.msg)
        }
        reject("error")
      }).catch((error) => {
        reject(error)
      })
    })
  },

  checkBatchUserDeliverResume({commit}, params) {
    return new Promise((resolve, reject) => {
      checkBatchUserDeliverResume(params).then(r => {
        if (r.data.code === 1) {
          resolve(r.data.data)
        } else if (r.data.code === 0) {
          reject(r.data.msg)
        }
        reject("已有用户更改记录！")
      }).catch((error) => {
        reject(error)
      })
    })
  },

  getUserDeliverInfoById({commit} ,id) {
    return new Promise((resolve, reject) => {
      getUserDeliverInfoById(id)
        .then((resp) => {
          if (resp.data.code === 1) {
            resolve(resp.data.data)
          }
          reject("error")
        }).catch((error) => reject(error))
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
